const express = require("express");
const mongoose = require('mongoose');
const app = express();
const promise = new Promise((resolve, reject) => {
    mongoose.set('useCreateIndex', true);
    mongoose.connect("mongodb://localhost:27017/mongoose_test", {
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    mongoose.connection.once('open', err => {
        if (!err) {
            console.log('数据库连接成功！！');
            resolve();
        } else {
            console.log(err);
            reject(err);
        }
    })
});
(async () => {
    await promise; 
    const Schema = mongoose.Schema; 
    const studentsSchema = new Schema({
        name: {
            type: String,
            unique: false, 
            required: true, 
            default: '栋儿啊'
        },
        age: Number,
        sex: String,
        stu_id: {
            type: String,
            unique: true, 
            required: true, 
        },
        createDate: {
            type: Date,
            default: Date.now()
        },
        info: Schema.Types.Mixed 
    }) 
    const Students = mongoose.model("students", studentsSchema);
    const s1 = new Students({
        name: 'tom',
        age: 12,
        sex: '男',
        stu_id: '03',
        createDate: Date.now(),
        info: '吃饭睡觉'
    })
    s1.save(err => {
        if (!err) console.log('数据保存成功');
        else console.log(err);
    })
})();











app.get("/test", (req, res) => {
    res.send("xxx");
});
app.listen(8080, err => {
    if (!err) console.log("server启动");
    else console.log(err);
});
// 1.初始化服务器
// 2.将数据库和服务器联系
// mongoose@5